package com.meet.service;

import com.meet.dao.DepartmentDao;
import com.meet.dao.DepartmentDaoImpl;
import com.meet.entity.BoardRoom;
import com.meet.entity.Department;
import com.meet.entity.PageBean;

import java.sql.SQLException;
import java.util.List;

/**
 * @author RicardoZeng
 * @date 2022/8/11 20:32
 */
public class DepartmentServiceImpl implements DepartmentService{
    DepartmentDao departmentDao = new DepartmentDaoImpl();

    public DepartmentServiceImpl(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    @Override
    public List<Department> getAllDepartment(int currentPage, int rows) throws SQLException {
        return departmentDao.getAllDepartment(currentPage, rows);
    }

    @Override
    public int addDepartment(Department department) throws SQLException {
        if(department == null)
            throw new SQLException("对象为空！！！");
        return departmentDao.addDepartment(department);
    }

    @Override
    public int delDepartment(Integer Id) throws SQLException {
        if(Id <= 0)
            throw new SQLException("Id 不能小于0！！！");
        return departmentDao.delDepartment(Id);
    }

    @Override
    public int updateDepartment(Department department) throws SQLException {
        if(department == null)
            throw new SQLException("对象为空！！！");
        return departmentDao.updateDepartment(department);
    }

    @Override
    public Department getDptId(Integer Id) throws SQLException {
        if(Id <= 0)
            throw new SQLException("Id 不能小于0！！！");
        return departmentDao.getDptId(Id);
    }

    @Override
    public PageBean<Department> getAllDptByPage(String _currentPage, String _rows) throws SQLException {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if(currentPage <=0) {
            currentPage = 1;
        }

        //1.创建空的PageBean对象
        PageBean<Department> pb = new PageBean<Department>();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = departmentDao.count();
        pb.setTotalCount(totalCount);

        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<Department> list = departmentDao.getAllDepartment(start,rows);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows)  == 0 ? totalCount/rows : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }

    @Override
    public List<Department> getDptName() throws SQLException {
        return departmentDao.getDptName();
    }
}
